#!/bin/sh

###############################################################################
##
##  HIGH VERBOSITY EXAMPLE
##
###############################################################################

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use cp.x to perform molecular dynamics"
$ECHO "in the presence of an electric field described through the"
$ECHO "modern theory of the polarization. The example shows how to"
$ECHO "calculate high-frequency and static dielectric constants and"
$ECHO "Born effective charges."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="cp.x"
PSEUDO_LIST="O.pz-rrkjus.UPF Mg.pz-n-vbc.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE \
                http://www.quantum-espresso.org/pseudo/1.3/UPF/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
CP_COMMAND="$PARA_PREFIX $BIN_DIR/cp.x $PARA_POSTFIX"
$ECHO
$ECHO "  running cp.x as: $CP_COMMAND"
$ECHO

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

# molecular dynamics calculation
cat > mgo.cp.cg.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="cp" prefix="mgo">

	<cell type="qecell">
		<qecell ibrav="1" alat="15.912">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="2">
		<specie name="O">
			<property name="mass">
				<real>12.01</real>
			</property>
			<property name="pseudofile">
				<string>O.pz-rrkjus.UPF</string>
			</property>
			<property name="ion_radius">
				<real>0.8d0</real>
			</property>
		</specie>
		<specie name="Mg">
			<property name="mass">
				<real>24.30</real>
			</property>
			<property name="pseudofile">
				<string>Mg.pz-n-vbc.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="bohr" nat="64" >
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   0.066300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   0.066300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000  15.978300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000  15.978300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000  15.978300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000  15.978300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000  15.978300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000  15.978300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000  15.978300000
				</real>
			</position>
		</atom>		
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000  15.978300000
				</real>
			</position>
		</atom>									
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>
		
		<parameter name="iprint">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="tstress">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="tprnfor">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="startingwfc">
			<string>
				random
			</string>
		</parameter>
		
		<parameter name="ion_positions">
			<string>
				from_input
			</string>
		</parameter>
	
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				20.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				160.0
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.d-8
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="nbnd">
			<integer>
				128
			</integer>
		</parameter>

	</field>
	
	<field name="Fields">

		<parameter name="nspin">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="tefield">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="efield">
			<real>
				0.0
			</real>
		</parameter>
		
	</field>
	
	<field name="CP">
	
<!--nstep, cell_dynamics, ion_dynamics e ion_temperature esistono anche nel PW 
ma nn sapevo in che field metterli  -->
		<parameter name="nstep">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="ion_dynamics">
			<string>
				none
			</string>
		</parameter>
		
		<parameter name="ion_temperature">
			<string>
				not_controlled
			</string>
		</parameter>
		
		<parameter name="cell_dynamics">
			<string>
				none
			</string>
		</parameter>
		
<!-- Da qui in poi esistono solo in CP-->
		
		<parameter name="isave">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="nr1b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="nr2b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="nr3b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="emass">
			<real>
				1000.d0
			</real>
		</parameter>
		
		<parameter name="emass_cutoff">
			<real>
				4.d0
			</real>
		</parameter>
		
		<parameter name="orthogonalization">
			<string>
				Gram-Schmidt
			</string>
		</parameter>
		
		<parameter name="ampre">
			<real>
				0.02
			</real>
		</parameter>
		
		<parameter name="tcg">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="passop">
			<real>
				0.3
			</real>
		</parameter>
		
		<parameter name="maxiter">
			<integer>
				250
			</integer>
		</parameter>
		
<!-- non esiste nel CP Help ion_damping -->		
		<parameter name="ion_damping">
			<real>
				0.0
			</real>
		</parameter>
<!--  -->

		<parameter name="greasp">
			<real>
				1.0
			</real>
		</parameter>
		
		<parameter name="epol">
			<integer>
				3
			</integer>
		</parameter>
			
	</field>
	
</input>
EOF
$ECHO "  running the CG calculation for bulk MgO E_field=0. a.u. ...\c"
$CP_COMMAND < mgo.cp.cg.xml > mgo.cp.cg.out
check_failure $?
$ECHO " done"

cat > mgo.cp.cg.efield.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="cp" prefix="mgo">

	<cell type="qecell">
		<qecell ibrav="1" alat="15.912">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="2">
		<specie name="O">
			<property name="mass">
				<real>12.01</real>
			</property>
			<property name="pseudofile">
				<string>O.pz-rrkjus.UPF</string>
			</property>
			<property name="ion_radius">
				<real>0.8d0</real>
			</property>
		</specie>
		<specie name="Mg">
			<property name="mass">
				<real>24.30</real>
			</property>
			<property name="pseudofile">
				<string>Mg.pz-n-vbc.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="bohr" nat="64" >
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   0.066300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   0.066300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000  15.978300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000  15.978300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000  15.978300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000  15.978300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000  15.978300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000  15.978300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000  15.978300000
				</real>
			</position>
		</atom>		
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000  15.978300000
				</real>
			</position>
		</atom>									
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
		<parameter name="restart_mode">
			<string>
				restart
			</string>
		</parameter>
		
		<parameter name="iprint">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="tstress">
			<logical>
				FALSE
			</logical>
		</parameter>
		
		<parameter name="tprnfor">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="startingwfc">
			<string>
				random
			</string>
		</parameter>
		
		<parameter name="ion_positions">
			<string>
				from_input
			</string>
		</parameter>
	
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				20.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				160.0
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.d-8
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="nbnd">
			<integer>
				128
			</integer>
		</parameter>

	</field>
	
	<field name="Fields">

		<parameter name="nspin">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="tefield">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="efield">
			<real>
				0.001
			</real>
		</parameter>
		
	</field>
	
	<field name="CP">
	
<!--nstep, cell_dynamics, ion_dynamics e ion_temperature esistono anche nel PW 
ma nn sapevo in che field metterli  -->
		<parameter name="nstep">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="ion_dynamics">
			<string>
				none
			</string>
		</parameter>
		
		<parameter name="ion_temperature">
			<string>
				not_controlled
			</string>
		</parameter>
		
		<parameter name="cell_dynamics">
			<string>
				none
			</string>
		</parameter>
		
<!-- Da qui in poi esistono solo in CP-->
		
		<parameter name="isave">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="nr1b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="nr2b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="nr3b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="emass">
			<real>
				1000.d0
			</real>
		</parameter>
		
		<parameter name="emass_cutoff">
			<real>
				4.d0
			</real>
		</parameter>
		
		<parameter name="orthogonalization">
			<string>
				Gram-Schmidt
			</string>
		</parameter>
		
		<parameter name="ampre">
			<real>
				0.02
			</real>
		</parameter>
		
		<parameter name="tcg">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="passop">
			<real>
				0.3
			</real>
		</parameter>
		
		<parameter name="maxiter">
			<integer>
				250
			</integer>
		</parameter>
		
<!-- non esiste nel CP Help ion_damping -->		
		<parameter name="ion_damping">
			<real>
				0.0
			</real>
		</parameter>
<!--  -->

		<parameter name="greasp">
			<real>
				1.0
			</real>
		</parameter>
		
		<parameter name="epol">
			<integer>
				3
			</integer>
		</parameter>
			
	</field>
	
</input>
EOF
$ECHO "  running the CG calculation for bulk MgO E_field=0.001 a.u. ...\c"
$CP_COMMAND < mgo.cp.cg.efield.xml > mgo.cp.cg.efield.out
check_failure $?
$ECHO " done"

cat > mgo.cp.damp.efield.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>


<input calculation="cp" prefix="mgo">

	<cell type="qecell">
		<qecell ibrav="1" alat="15.912">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="2">
		<specie name="O">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>O.pz-rrkjus.UPF</string>
			</property>
		</specie>
		<specie name="Mg">
			<property name="mass">
				<real>1.0</real>
			</property>
			<property name="pseudofile">
				<string>Mg.pz-n-vbc.UPF</string>
			</property>
		</specie>
	</atomic_species>

	<atomic_list units="bohr" nat="64" >
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   0.066300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   0.066300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   0.066300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>		
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="Mg">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   0.066300000  15.978300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   4.044300000  15.978300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>			
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					4.044300000   8.022300000  15.978300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					0.066300000  12.000300000  15.978300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000   4.044300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000   8.022300000
				</real>
			</position>
		</atom>
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   0.066300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   4.044300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   0.066300000  15.978300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   4.044300000  15.978300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000   4.044300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000   8.022300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000   8.022300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000  12.000300000  12.000300000
				</real>
			</position>
		</atom>	
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					12.000300000   8.022300000  15.978300000
				</real>
			</position>
		</atom>		
		<atom name="O">
			<position>
				<real rank="1" n1="3">
					8.022300000  12.000300000  15.978300000
				</real>
			</position>
		</atom>									
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
		<parameter name="restart_mode">
			<string>
				restart
			</string>
		</parameter>
		
		<parameter name="iprint">
			<integer>
				100
			</integer>
		</parameter>
		
		<parameter name="tstress">
			<logical>
				FALSE
			</logical>
		</parameter>
		
		<parameter name="tprnfor">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="startingwfc">
			<string>
				random
			</string>
		</parameter>
	
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				20.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				160.0
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="nbnd">
			<integer>
				128
			</integer>
		</parameter>

	</field>
	
	<field name="Fields">

		<parameter name="nspin">
			<integer>
				1
			</integer>
		</parameter>
		
		<parameter name="tefield">
			<logical>
				TRUE
			</logical>
		</parameter>
		
		<parameter name="efield">
			<real>
				0.001
			</real>
		</parameter>
		
	</field>
	
	<field name="CP">
	
<!--nstep, dt, cell_dynamics, e ion_dynamics esistono anche nel PW 
ma nn sapevo in che field metterli  -->
		<parameter name="nstep">
			<integer>
				20
			</integer>
		</parameter>
		
		<parameter name="dt">
			<real>
				10.0
			</real>
		</parameter>
		
		<parameter name="ion_dynamics">
			<string>
				verlet
			</string>
		</parameter>
		
		<parameter name="cell_dynamics">
			<string>
				none
			</string>
		</parameter>
		
<!-- Da qui in poi esistono solo in CP-->
		
		<parameter name="isave">
			<integer>
				100
			</integer>
		</parameter>
		
		<parameter name="nr1b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="nr2b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="nr3b">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="emass">
			<real>
				250.d0
			</real>
		</parameter>
		
		<parameter name="emass_cutoff">
			<real>
				3.d0
			</real>
		</parameter>
		
		<parameter name="ampre">
			<real>
				0.02
			</real>
		</parameter>
		
<!-- non esiste nel CP Help ion_damping -->		
		<parameter name="ion_damping">
			<real>
				0.0
			</real>
		</parameter>
<!--  -->
		
		<parameter name="epol">
			<integer>
				3
			</integer>
		</parameter>
		
		<parameter name="electron_dynamics">
			<string>
				damp
			</string>
		</parameter>
		
		<parameter name="ortho_max">
			<integer>
				30
			</integer>
		</parameter>
		
		<parameter name="electron_damping">
			<real>
				0.05
			</real>
		</parameter>
			
	</field>
	
</input>
EOF
$ECHO "  running the CP- damped MD calculation for bulk MgO E_field=0.001 a.u. ...\c"
$CP_COMMAND < mgo.cp.damp.efield.xml > mgo.cp.damp.efield.out
check_failure $?
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR : done"
